//
//  BillListTableCell.swift
//  LGMusic
//
//  Created by 梁尚嘉 on 2017/3/22.
//  Copyright © 2017年 LimeGah. All rights reserved.
//

import UIKit
import SDWebImage

@objc protocol BillListTableCellDelegate {
    @objc optional
    func onFavoriteButtonClick(_ button:UIButton,_ indexPath:IndexPath) -> Void
}

class BillListTableCell : SongBaseTableCell  {
    public var delegate:BillListTableCellDelegate?
    fileprivate var favoriteBtn: UIButton!
    fileprivate var isFavorite: Bool?
    fileprivate var bill:Bill?

    override func configure(_ Object: AnyObject) {
        let bill:Bill = Object as! Bill
        self.bill = bill
        self.iconImageView.sd_setImage(with: URL.init(string: bill.pic_small!), placeholderImage: UIImage(named:"ic_song_placeholder"), options: SDWebImageOptions.continueInBackground)
        self.artistNameLb.text = bill.artist_name;
        self.songNameLb.text = bill.title;
        self.isFavorite = bill.isFavorite;
        self.favoriteBtn.isSelected = isFavorite!
    }

    override func configure() {
        super.configure()
        self.favoriteBtn = UIButton.init()
        self.favoriteBtn.setImage(UIImage.init(named: "bt_radioplay_contrl_heart_normal"), for: UIControlState.normal)
        self.favoriteBtn.setImage(UIImage.init(named: "bt_radioplay_contrl_heart_press"), for: UIControlState.highlighted)
        self.favoriteBtn.setImage(UIImage.init(named: "bt_radioplay_contrl_heart_hl"), for: UIControlState.selected)
        self.favoriteBtn.addTarget(self, action:#selector(BillListTableCell.favoriteButtonTap(_:)), for: UIControlEvents.touchUpInside)
        let margin = SM_SIZE(15)
        self.favoriteBtn.contentEdgeInsets = UIEdgeInsets.init(top: margin, left: margin, bottom: margin, right: margin)
        self.contentView.addSubview(self.favoriteBtn)
        self.favoriteBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-SM_SIZE(40))
            make.centerY.equalTo(self.contentView.snp.centerY)
            make.width.height.equalTo(SM_SIZE(120))
        }

    }

    //MARK: Actions
    func favoriteButtonTap(_ sender:UIButton) {
        self.favoriteBtn.isSelected = !self.favoriteBtn.isSelected
        if (self.delegate != nil) {
            let tableview = self.superview?.superview as! UITableView
            self.delegate?.onFavoriteButtonClick!(sender,tableview.indexPath(for: self)!)
        }
    }
}


class SongInPlayTableCell:SongBaseTableCell {

    fileprivate var playingIcon: UIImageView?
    fileprivate var playingBackGround: UIView?
    public var isPlaying: Bool?{
        didSet {
            if (oldValue != isPlaying) {
                self.playingIcon?.isHidden = !isPlaying!
                self.playingBackGround?.isHidden = !isPlaying!
            }
        }
    }

    override func configure(_ Object: AnyObject) {
        let song:Song = Object as! Song
        self.iconImageView.sd_setImage(with: URL.init(string: song.songPicBig!), placeholderImage: nil, options: SDWebImageOptions.continueInBackground)
        self.artistNameLb.text = song.artistName;
        self.songNameLb.text = song.songName;
    }

    override func configure() {
        super.configure()

        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear

        self.playingBackGround = UIView()
        self.playingBackGround?.backgroundColor = SM_HEXCOLOR("FBFBFB").withAlphaComponent(0.5)
        self.contentView.insertSubview(self.playingBackGround!, at: 0)
        self.playingBackGround?.snp.makeConstraints({ (make) in
            make.edges.equalTo(0)
        })

        self.playingIcon = UIImageView.init(image: UIImage.init(named: "ic_inPlay_playing"));
        self.contentView.addSubview(self.playingIcon!)
        self.playingIcon?.snp.makeConstraints({ (make) in
            make.right.equalTo(SM_SIZE(-30))
            make.bottom.equalTo(self.artistNameLb.snp.bottom)
        })

        let separatorline = UIView()
        separatorline.backgroundColor = SM_HEXCOLOR("#E1D6D6")
        self.contentView.addSubview(separatorline)
        separatorline.snp.makeConstraints { (make) in
            make.left.equalTo(SM_SIZE(20))
            make.right.equalTo(SM_SIZE(-20))
            make.bottom.equalTo(0)
            make.height.equalTo(1/UIScreen.main.scale)
        }
    }


}


class SongBaseTableCell: BaseTableCell  {

    fileprivate var iconImageView: UIImageView!
    fileprivate var artistNameLb: UILabel!
    fileprivate var songNameLb: UILabel!

    override func configure() {
        super.configure()

        self.iconImageView = UIImageView.init()
        self.contentView.addSubview(self.iconImageView)
        self.iconImageView.snp.makeConstraints { (make) in
            make.left.equalTo(SM_SIZE(35))
            make.top.equalTo(SM_SIZE(20))
            make.bottom.equalTo(SM_SIZE(-20))
            make.width.equalTo(self.iconImageView.snp.height)
        }

        self.songNameLb = UILabel.init()
        self.songNameLb.textColor = UIColor.black
        self.songNameLb.font = SM_CFont(19)
        self.contentView.addSubview(self.songNameLb)
        self.songNameLb.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconImageView.snp.right).offset(SM_SIZE(20))
            make.top.equalTo(self.iconImageView.snp.top).offset(SM_SIZE(5))
            make.right.equalTo(SM_SIZE(-30-20-20))
        }

        self.artistNameLb = UILabel.init()
        self.artistNameLb.textColor = UIColor.gray
        self.artistNameLb.font = SM_CFont(16)
        self.contentView.addSubview(self.artistNameLb)
        self.artistNameLb.snp.makeConstraints { (make) in
            make.left.equalTo(self.songNameLb.snp.left)
            make.top.equalTo(self.songNameLb.snp.bottom).offset(SM_SIZE(5))
        }

    }

    override class func cellHeight() -> CGFloat {
        return SM_SIZE(130);
    }

}
